package ias.tray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class JeusTray implements Runnable {

    private static BufferedReader consoleView;

    public static void main(String[] args) {
        if (SystemTray.isSupported()) {
            SystemTray systemTray = SystemTray.getSystemTray();
            final Image downImage = Toolkit.getDefaultToolkit().getImage(
                    "relaunchf.gif");
            final Image bootImage = Toolkit.getDefaultToolkit().getImage(
                    "relaunch.gif");
            final String jeusHome = args[0];
            final String node = args[1];
            final String username = args[2];
            final String password = args[3];
            final String bootCommand = jeusHome
                    + File.separator
                    + "bin"
                    + File.separator
                    + (System.getProperty("os.name").toLowerCase().startsWith(
                            "window") ? "jeus.bat" : "jeus");
            final String downCommand = jeusHome
                    + File.separator
                    + "bin"
                    + File.separator
                    + (System.getProperty("os.name").toLowerCase().startsWith(
                            "window") ? "jeus-down.bat" : "jeus-down");
            PopupMenu popup = new PopupMenu();
            final TrayIcon trayIcon = new TrayIcon(downImage, "JEUS control",
                    popup);
            MenuItem bootItem = new MenuItem("boot");
            bootItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("boot");
                    ProcessBuilder pb = new ProcessBuilder(bootCommand, "-U"
                            + username, "-P" + password);
                    pb.redirectErrorStream(true);
                    try {
                        Map<String, String> name = pb.environment();
                        name.put("JEUS_HOME", jeusHome);
                        Process process = pb.start();
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(process.getInputStream()));
                        consoleView = reader;
                        new Thread(new JeusTray()).start();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    trayIcon.setImage(bootImage);
                }
            });
            popup.add(bootItem);
            MenuItem downItem = new MenuItem("down");
            downItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("down");
                    ProcessBuilder pb = new ProcessBuilder(downCommand, node,
                            "-U" + username, "-P" + password, "jeusexit");
                    pb.redirectErrorStream(true);
                    try {
                        Map<String, String> name = pb.environment();
                        name.put("JEUS_HOME", jeusHome);
                        pb.start();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    trayIcon.setImage(downImage);
                }
            });
            popup.add(downItem);
            MenuItem exitItem = new MenuItem("exit");
            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("exit");
                    System.exit(0);
                }
            });
            popup.add(exitItem);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        }
    }

    public void run() {
        String line;
        try {
            while ((line = consoleView.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
